import pygame as SDL
from pygame.locals import *
import os,sys

class StateMachine:
    """A basic state machine.  Nothing complicated.
    It's very similar to a stack."""
    def __init__(self):
        self.llist = []
    def push(self,state):
        self.llist.append(state)
    def pop(self):
        self.llist.pop()
    def top(self):
        #Not sure this will ever be needed, but let's add
        #it just in case, to be thorough
        return self.llist[-1]
    def isEmpty(self):
        if len(self.llist) > 0: return False
        return True
    def run(self, **args):
        self.llist[-1].state(args)


class Sprite(SDL.sprite.Sprite):
    """Basic sprite class.  The default pygame Sprite class
    is far too limited.  We'll extend it here."""
    def __init__(self,name,x,y):
        SDL.sprite.Sprite.__init__(self)
        
        
        self.image = self.__load_image(name)
        self.rect = self.image.get_rect()
        
        self.width = self.rect.width
        self.height = self.rect.height
        self.x = x
        self.y = y
        self.moveTo(x,y)
        
    def __load_image(self,name):
        try:
            image = SDL.image.load(name)
        except SDL.error,message:
            print "Cannot load image:",name
            raise SystemExit,message
        
        if image.get_flags() == SRCALPHA:
            image = image.convert_alpha()
        else:
            image = image.convert()
        
        return image
        
    def draw(self,screen):
        screen.blit(self.image,self.rect)
        
    def moveTo(self,x,y):
        self.x = x
        self.y = y
        self.rect = SDL.rect.Rect(x,y,self.width,self.height)


class Background:
    """A quick background class.  This could easily be improved."""
    def __init__(self,context,x,y,z):
        self.image = SDL.Surface(context.get_size())
        self.image = self.image.convert()
        self.image.fill((x,y,z))
        
    def draw(self,screen):
        screen.blit(self.image,(0,0))
        